# 1.导包
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression,SGDRegressor
from sklearn.metrics import mean_squared_error

# 2.获取数据
data =load_boston()
# print(data.data)
# print(data.target)

# 版本问题导致的用下面这些代码
# import pandas as pd
# import numpy as np
#
# data_url = "http://lib.stat.cmu.edu/datasets/boston"
# raw_df = pd.read_csv(data_url, sep="\s+", skiprows=22, header=None)
# data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
# target = raw_df.values[1::2, 2]
# 3.划分数据
x_train,x_test,y_train,y_test=train_test_split(data.data,data.target,test_size=0.2,random_state=22)
# 4.特征工程
transfer=StandardScaler()
x_train=transfer.fit_transform(x_train)
x_test=transfer.transform(x_test)
# 5.模型训练
# 正规方程
LR = LinearRegression()
# 梯度下降
# LR = SGDRegressor(max_iter=100,learning_rate='constant',eta0=0.001)
LR.fit(x_train,y_train)
print(LR.intercept_)
print(LR.coef_)
# 6.模型预测
y_predict =LR.predict(x_test)
print(y_predict)
# 7.模型评估
print(mean_squared_error(y_predict, y_test))


